package com.dudu.dao;

import java.sql.Connection;
import java.util.List;

import net.sf.json.JSONObject;

import com.dudu.bean.SessionIdBean;

import core.CoreDao;
import core.db.DBConnectionUtil;

/**
 * SessionId关联关系Dao类
 * @author jack
 *
 */
public class SessionIDDao {
	
	/**
	 * 新增
	 * @param dao
	 * @param bean
	 * @return
	 */
	public int insert(CoreDao dao, SessionIdBean bean) {

		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					"INSERT INTO t_session_id(dudu_session_id, cass_session_id, create_time)");
			sql.append("VALUES(?, ?, sysdate())");

			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] { bean.getDudu_session_id(),
					bean.getCass_session_id() });

			JSONObject obj = list.get(0);
			result = obj.optInt("result");

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	/**
	 * 新增
	 * @param dao
	 * @param bean
	 * @return
	 */
	public int insert(CoreDao dao, String duduSessionId) {

		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					"INSERT INTO t_session_id(dudu_session_id, cass_session_id, create_time)");
			sql.append("VALUES(?, ?, sysdate())");

			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] { duduSessionId, "" });

			JSONObject obj = list.get(0);
			result = obj.optInt("result");

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	/**
	 * 根据duduSessionId查询
	 * @param dao
	 * @param duduSessionId
	 * @return
	 */
	public SessionIdBean queryByDuduSessionId(CoreDao dao, String duduSessionId) {

		Connection con = null;
		SessionIdBean result = null;

		try {
			con = DBConnectionUtil.getDBConnection("emate1");

			StringBuffer sql = new StringBuffer("select * FROM t_session_id where dudu_session_id=?");
			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] { duduSessionId });
			if (list != null && list.size() > 0) {
				List<SessionIdBean> items = dao.covertToBean(list, SessionIdBean.class);
				result = items.get(0);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	/**
	 * 根据cassSessionID查询
	 * @param dao
	 * @param cassSessionId
	 * @return
	 */
	public SessionIdBean queryByCassSessionId(CoreDao dao, String cassSessionId) {

		Connection con = null;
		SessionIdBean result = null;

		try {
			con = DBConnectionUtil.getDBConnection("emate1");

			StringBuffer sql = new StringBuffer("select * FROM t_session_id where cass_session_id=?");
			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] { cassSessionId });
			if (list != null && list.size() > 0) {
				List<SessionIdBean> items = dao.covertToBean(list, SessionIdBean.class);
				result = items.get(0);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	/**
	 * 根据DuuSessionId更新cassSessionID
	 * @param dao
	 * @param bean
	 * @return
	 */
	public int updateByDuduSessionId(CoreDao dao, SessionIdBean bean) {

		Connection con = null;
		int result = -1;

		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(" UPDATE t_session_id set cass_session_id=? where dudu_session_id=? ");

			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] { bean.getCass_session_id(),
					bean.getDudu_session_id() });

			if (list != null && list.size() > 0) {
				list.get(0);
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	/**
	 * 根据duduSessionID更新cassSessionID
	 * @param dao
	 * @param bean
	 * @return
	 */
	public int updateByCassSessionId(CoreDao dao, SessionIdBean bean) {

		Connection con = null;
		int result = -1;

		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(" UPDATE t_session_id set dudu_session_id=? where cass_session_id=? ");

			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] { bean.getDudu_session_id(),
					bean.getCass_session_id() });

			if (list != null && list.size() > 0) {
				list.get(0);
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
}
